home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: #define "creating" strings ?
- Date: 15 Mar 1996 17:51:43 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Mar15105143@qcd.lanl.gov>
- References: <Do9tsI.H2t@undergrad.math.uwaterloo.ca> <4ia60f$dhq@ringer.cs.utsa.edu>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: nreitzel@lonestar.jpl.utsa.edu's message of 14 Mar 1996 22:18:55 GMT
-
- In article <4ia60f$dhq@ringer.cs.utsa.edu>
- nreitzel@lonestar.jpl.utsa.edu (Norman L. Reitzel ) writes:
-
- NLR: In article <Do9tsI.H2t@undergrad.math.uwaterloo.ca>
- crpalmer@solo.uwaterloo.ca (Chris Palmer) writes:
- NLR: >I have a problem that I would like to solve using a "feature" of the C
- NLR: >preprocessor that until now I've never seen or heard of. A book that we have
- NLR: >here gives the example:
- NLR: >
- NLR: >#define MAKESTRING(x) "x"
- NLR: >
- NLR: >which does in fact produce strings that have the value of x substituted.
- NLR: >[eg: MAKESTRING(foo) gets processed into "foo"].
- NLR: >
- NLR: >I've verified this on a DEC Alpha (cc) and the GNU C compiler..
-
- You probably didn't tell GNU C to compile C. For example `gcc
- -traditional' compiles a language which was a predecessor of C: it is
- now commonly called `traditional C' or `K&R I C'. `gcc' alone usually
- compiles GNU C. Only when you say `gcc -ansi -pedantic' does it try
- to compile C.
-
- NLR: >
- NLR: >Is this in fact a supported behaviour that should be portable to most
- NLR: >C compilers?
- NLR:
- NLR: Sure. Why is this surprizing? The preprocessor performs character
- NLR: string substitution, a la M4. It doesn't care if it's assembling a
- NLR: string with quotes or a syntactic disaster.
-
- This was the behaviour of older compilers, but it is not true in the
- language currently called C.
-
- The preprocessor does not perform character string substitution. (In
- fact I do not even know what you mean by that: after #define h j, can
- I write "hello" in my code instead of "jello"? Think before you
- explain incorrectly).
-
- Preprocessing works on what are called preprocessing tokens. It
- replace one set of pp-tokens with another ... though the new set
- of _pp-tokens_ may indeed not form a syntactically valid C fragment.
-
- Preprocessing does not change `parts' of pp-tokens. An entire string
- is a single pp-token and hence its parts can't be changed.
-
- #define MAKESTRING(x) #x
-
- will, in any ANSI conformant system, have the desired result. Read the
- FAQ for more details.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-